﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace MenuGame2DXNA
{
    class HighScoreDAL
    {
        private static int CompareScore(Score x, Score y)
        {
            return y.Diem - x.Diem;
        }
        public static List<Score> getListScore()
        {
            List<Score> tempList = new List<Score>();

            XDocument doc = XDocument.Load("HighScore.xml");
            
            foreach (var node in doc.Element("HighScore").Elements("HS"))
            {
                tempList.Add(new Score(node.Attribute("ten").Value, Int32.Parse(node.Attribute("diem").Value)));
            }
            tempList.Sort(CompareScore);
            List<Score> result = new List<Score>();
            int Min = Math.Min(tempList.Count, 10);
            for (int i = 0; i < Min; i++)
                result.Add(tempList[i]);
                return result;
          
        }
         
        public static void addScore(Score sc)
        {
          
            XmlDocument doc = new XmlDocument();
            doc.Load("HighScore.xml");

            XmlElement elm = doc.CreateElement("HS");

            elm.SetAttribute("ten", sc.Ten);
            elm.SetAttribute("diem", sc.Diem.ToString());

            doc.DocumentElement.AppendChild(elm);

            doc.Save("HighScore.xml");
        }
    }

    class Score
    {
        private string ten;

        public string Ten
        {
            get { return ten; }
            set { ten = value; }
        }
        private int diem;

        public int Diem
        {
            get { return diem; }
            set { diem = value; }
        }

        public Score(string ten_in, int diem_in)
        {
            ten = ten_in;
            diem = diem_in;
        }
    }
}
